#import "CTPasswordView.h"
@interface CTPasswordView ()
@property (nonatomic ,copy) NSString *placeHolder;
@property (nonatomic ,strong) NSDate *lastDate;
@end
@implementation CTPasswordView
- (instancetype)init {
    return [self initWithPlaceHolder:@""];
}
- (instancetype)initWithPlaceHolder:(NSString *)placeHolder {
    if (self = [super init]) {
        self.placeHolder = placeHolder;
        [self setUI];
    }
    return self;
}
#pragma mark - private
- (void)setUI
{
    self.tfModel = [[CTTFModel alloc] init];
    [self addSubview:self.backView];
    [self.backView addSubview:self.leftBtn];
    [self.backView addSubview:self.rightBtn];
    [self.backView addSubview:self.contentTF];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self);
    }];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView).offset(Ratio(8));
        make.centerY.equalTo(self.backView);
        make.size.mas_equalTo(CGSizeMake(Ratio(34), Ratio(34)));
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backView).offset(Ratio(-8));
        make.centerY.equalTo(self.backView);
        make.size.mas_equalTo(CGSizeMake(Ratio(34), Ratio(34)));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftBtn.mas_right).offset(Ratio(10));
        make.right.mas_equalTo(self.rightBtn.mas_left).offset(Ratio(-10));
        make.centerY.equalTo(self->_backView);
    }];
}
- (void)callKeyBoard
{
    [self.contentTF becomeFirstResponder];
}
- (UITextField *)contentTF
{
    if (_contentTF == nil) {
        _contentTF = [[UITextField alloc] init];
        _contentTF.keyboardType = UIKeyboardTypeDefault;
        _contentTF.font = [UIFont systemFontOfSize:Ratio(12) weight:UIFontWeightMedium];
        NSString *holderText = !isEmpty(self.placeHolder) ? self.placeHolder : @""; 
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttributes:@{NSFontAttributeName:
                                     [UIFont systemFontOfSize:Ratio(16)],
                                     NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#77777D"]
        }
                            range:NSMakeRange(0, holderText.length)];
        _contentTF.attributedPlaceholder = placeholder;
        _contentTF.textColor = [UIColor color77777D];
        _contentTF.secureTextEntry = YES;
        @weakify(self);
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
            @strongify(self);
            if (self.editingBlock) {
                self.editingBlock(self, self.contentTF.text);
            }
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%.2f:%@:0,",interval, self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
            @strongify(self);
            self.lastDate = [NSDate date];
            if (self.tfModel.dwellDuration == 0) {
                self.tfModel.initiationTimestamp = [self.lastDate timeIntervalSince1970];
            }
            NSString *newLog = [NSString stringWithFormat:@"%d:%@:0,",0,  self.contentTF.text].mutableCopy;
            [self.tfModel.valueParameter appendString:newLog];
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
            @strongify(self);
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%f:%@:2,",interval,  self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            self.tfModel.conclusionTimestamp = [currentDate timeIntervalSince1970];
            self.tfModel.dwellDuration = [NSDate timeIntervalWithStartInterval:self.tfModel.initiationTimestamp endInterval:self.tfModel.conclusionTimestamp];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
            if (self.editDidEndBlock) {
                self.editDidEndBlock(self, self.tfModel);
            }
        }];
    }
    return _contentTF;
}
- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor colorF0F2F5];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = Ratio(22);
    }
    return _backView;
}
- (UIButton *)leftBtn
{
    if (_leftBtn == nil) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setImage:[UIImage imageNamed:@"login_lock"] forState:UIControlStateNormal];
        [_leftBtn setImage:[UIImage imageNamed:@"login_lock"] forState:UIControlStateSelected];
    }
    return _leftBtn;
}
- (UIButton *)rightBtn
{
    if (_rightBtn == nil) {
        _rightBtn = [[UIButton alloc] init];
        [_rightBtn setImage:[UIImage imageNamed:@"login_txt_hide"] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:@"login_txt_show"] forState:UIControlStateSelected];
        @weakify(self);
        [[_rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            self.contentTF.secureTextEntry = !self.contentTF.secureTextEntry;
            self.rightBtn.selected = !self.rightBtn.selected;
        }];
    }
    return _rightBtn;
}
@end
